<?php
namespace app\api\model;

    class Page {
        public $page;   //当前页
        public $total; //总记录数
        public $listRows; //每页显示记录数
        private $uri;//动态url
        public $pageNum; //总页数
        private $listNum=5;//显示页码按钮数量
        public $render;//分页后的html模板
        public $data;//分页后渲染到模板的数据
        /*
         * 初始化分页数据
         *$_count 数据总条数
         * $listRows 每页记录数
         */
        public function __construct($_count, $listRows=10){
            $this->total=$_count;
            $this->listRows=$listRows;
            $this->uri=$this->getUri();
            $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
            $this->pageNum=ceil($this->total/$this->listRows);
            $this->render=$this->pageHtml();
            //$this->data=array_slice($sdata,($this->page-1)*$this->listRows,$listRows);
            return $this->data;
        }

        //动态获取url
        private function getUri(){
            $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?");
            $parse=parse_url($url);

            if(isset($parse["query"])){
                parse_str($parse['query'],$params);
                unset($params["page"]);
                $url=$parse['path'].'?'.http_build_query($params);
            }

            return $url;
        }

        //首页
        private function first(){
			if($this->pageNum >= 6){
				$html = "";
				if($this->page <= 2)
					$html.=" ";
				else
					$html.=" <a class='btn btn-primary-outline'  href='javascript:;' num='1'>1</a>";

				return $html;
			}
        }

		
		//首页
        private function firsts(){
			if($this->pageNum >= 6){
				$html = "";
				if($this->page == 1)
					$html.=" ";
				else
					$html.=" <a class='btn btn-primary-outline'  href='javascript:;' num='1'> 首页 </a>";

				return $html;
			}
        }
        //上一页
        private function prev(){
            $html = "";
            if($this->page==1)
                //$html.=" <a href='javascript:;'><span class='icons icon-backward'></span></a>";
                $html.="";
            else
                $html.=" <a num='".($this->page-1)."' href='javascript:;'><span class='icons icon-backward'></span></a>";

            return $html;
        }

        //页码按钮
        private function pageList(){
            $linkPage="";
            $inum=floor($this->listNum/2);
            $inum_ceil = ceil($inum);
				if($this->page >= 4){
					$linkPage .= "<a href='javascript:;' style='background:#f3f3f3 !important;'>...</a>";
				}
            for($i = 1; $i <= $this->listNum; $i++){
				
			
                if(($this->page - $inum) <= 0 ){
                    $page = $i;
                }elseif(($this->page + $inum - 1) >= $this->pageNum){
                    $page = $this->pageNum - $this->listNum + $i;
                }else{
                    $page = $this->page - $inum_ceil + $i;
                }
                if($page > 0 && $page != $this->page){

                    if($page <= $this->pageNum){
                        $linkPage .= "<a num='{$page}' href='javascript:;'>" . $page . "</a>";
                    }else{
                        break;
                    }
                }else{
                    if($page > 0 && $this->pageNum != 1){
                        $linkPage .= "<a num=" . $page . " class='current' href='javascript:;'>" . $page . "</a>";
                    }
                }
            }
			if($this->pageNum >= 6 && ($this->page + 5) <= $this->pageNum){
				$linkPage .= "<a href='javascript:;' style='background:#f3f3f3 !important;'>...</a>";
			}
			
            return $linkPage;
        }

        //下一页
        private function next(){
            $html = "";
            if($this->page==$this->pageNum)
               // $html.=" <a href='javascript:;'><span class='icons icon-forward'></span></a>";
                $html.="";
            else
                $html.=" <a num='".($this->page+1)."' href='javascript:;'><span class='icons icon-forward'></span></a>";

            return $html;
        }

        //尾页
        private function last(){
			if($this->pageNum >= 6){
				$html = "";
				if($this->page >= $this->pageNum - 3)
					$html.=" ";
				else
					$html.=" <a class='btn btn-primary-outline' href='javascript:;' num='".($this->pageNum)."'>$this->pageNum</a>";

				return $html;
			}
        }
		 //尾页
        private function lasts(){
			if($this->pageNum >= 6){
				$html = "";
				if($this->page==$this->pageNum)
					$html.=" ";
				else
					$html.=" <a class='btn btn-primary-outline' href='javascript:;' num='".($this->pageNum)."'> 尾页 </a>";

				return $html;
			}
        }

        //输入指定页码
        private function goPage(){
			if($this->pageNum >= 6){
				return '  <span style="font-size: 16px;margin: 0 10px 0 20px;display:  inline-block;line-height:  40px;vertical-align:  middle;color:  #666;">跳转到:</span>
				<input id="go" type="number" style="width: 40px;height: 38px;border:  1px solid #e5e5e5;padding: 0 10px;" value="'.$this->page.'">
				<a id="GO"num="" href="javascript:;">Go</a>
				';
			}
        }

        //选择指定页码
        function selectPage(){
            $inum=10;
            $location = $this->uri.'&page=';
            $selectPage ="<span class='va-m'>到第 </span> <span class='select-box' style='width:initial'><select class='select' name='topage' size='1' onchange='window.location=\"$location\"+this.value'>";

            for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){
                if($i<=0){
                    continue;
                }
                if($i>$this->pageNum){
                    continue;
                }
                if($i == $this->page){
                    $selectPage .="<option value='$i' selected>$i</option>";
                }else{
                    $selectPage .="<option value='$i'>$i</option>";
                }
            }

            $selectPage .="</select></span> <span class='va-m'>页</span>";
            return $selectPage;
        }

        //组装分页的html模板
        function pageHtml(){
            $html  = "<div class='pagination' id='pagination'>";
			$html .= $this->firsts();
			
			
            $html .= $this->prev();
			$html .= $this->first();
			
            $html .= $this->pageList();
			$html .= $this->last();
			
            $html .= $this->next();
			
			$html .= $this->lasts();
			
			
            $html .= $this->goPage();
//            $html .= $this->selectPage();
            $html .= '</div>';

            return $html;
        }
    }
